package controllers;

import java.util.Collections;
import java.util.Set;

import models.Establishment;
import models.FoodCourt;
import models.Menu;
import models.Owner;
import models.SpotConfiguration;
import models.Telephone;
import play.data.Upload;
import play.mvc.Controller;
import play.mvc.With;

@With(Secure.class)
public class Store extends Controller {
	public static void list(long foodCourtId) {
		FoodCourt foodCourt = FoodCourt.findById(foodCourtId);
		render(foodCourt);
	}

	public static void blank(long foodCourtId) {
		render(foodCourtId);
	}

	public static void show(long id, long foodCourtId) {
		Establishment establishment = Establishment.findById(id);
		render(establishment, foodCourtId);
	}

	public static void delete(long id, long foodCourtId) {
		Establishment establishment = Establishment.findById(id);
		establishment.setRemoved(true);
		establishment.save();
		list(foodCourtId);
	}

	public static void create(Establishment establishment, FoodCourt foodCourt, Telephone telephone, Upload picture) {
		PlaceController.savePlace(establishment, telephone, picture);
		Owner user = Owner.find("from Admin u where u.email = ?", Security.connected()).first();
		user.getEstablishments().add(establishment);
		user.save();
		foodCourt.getEstablishments().add(establishment);
		foodCourt.save();
		SpotConfiguration configuration = new SpotConfiguration();
		configuration.setEstablishment(establishment);
		configuration.save();
		
		Menu menu = new Menu();
		menu.setEstablishment(establishment);
		menu.save();
		
		list(foodCourt.getId());
	}
	
	public static void save(Establishment establishment, FoodCourt foodCourt, Telephone telephone, Upload picture) {
		PlaceController.savePlace(establishment, telephone, picture);
		list(foodCourt.getId());
	}
}
